﻿using AjaxControlToolkit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Branch : System.Web.UI.Page
{
    CommonMethods commonMethod = new CommonMethods();
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        Response.Cache.SetValidUntilExpires(false);
        Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        if (!Page.IsPostBack)
        {
            BindCompanies();
            BindBranches();
            
        }
    }


    private void BindCompanies()
    {
        DDL_Company.DataSource = commonMethod.getAllCompanies();
        DDL_Company.DataBind();
    }
    protected void btnSaveBranch_Click(object sender, EventArgs e)
    {
        if (btnSaveBranch.Text == "Save")
        {
            using (POSEntities db = new POSEntities())
            {
                Branch branch = new Branch();
                branch.BranchName = txtBranch.Text;
                branch.City_ID = Convert.ToInt32(hdfCity.Value);
                branch.CompanyId = Convert.ToInt32(DDL_Company.SelectedValue);
                branch.EntryBy = 1;
                branch.EntryDate = DateTime.Now;
                branch.IsDeleted = false;
                db.Branches.Add(branch);
                db.SaveChanges();

            }
        }
        else if (btnSaveBranch.Text == "Update")
        {
            using (POSEntities db = new POSEntities())
            {
                Int32 id = Convert.ToInt32(btnSaveBranch.CommandArgument);
                var branch = (from b in db.Branches where (b.BranchId ==id ) select b).FirstOrDefault();
                branch.BranchName = txtBranch.Text;
                branch.City_ID = Convert.ToInt32(hdfCity.Value);
                branch.CompanyId = Convert.ToInt32(DDL_Company.SelectedValue);
                branch.ModifiedBy = 1;
                branch.ModifiedDate = DateTime.Now;
                db.SaveChanges();

            }
        }
        BindBranches();
        clearForm();
    }

    private void BindBranches()
    {
        GridViewBranches.DataSource = commonMethod.getBranchesWithCompanyId(Convert.ToInt32(DDL_Company.SelectedValue));
        GridViewBranches.DataBind();
    }
    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        clearForm();
    }

    protected void lbtnEditBranch_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;
        int cId = Convert.ToInt32(lbtn.CommandArgument);
        using (POSEntities db = new POSEntities())
        {
            var branch = (from b in db.Branches
                          join cit in db.Cities on b.City_ID equals cit.City_ID
                          join c in db.Companies on b.CompanyId equals c.CompanyId
                          where (b.BranchId == cId) && b.IsDeleted == false
                          select new { c.CompanyId, c.CompanyName, cit.City_ID, cit.City_Name, b.BranchId, b.BranchName }).FirstOrDefault();

            txtBranch.Text = branch.BranchName;
            txtCity.Text = branch.City_Name;
            hdfCity.Value = branch.City_ID.ToString();
            DDL_Company.SelectedValue = branch.CompanyId.ToString();
            btnSaveBranch.CommandArgument = branch.BranchId.ToString();
            btnSaveBranch.Text = "Update";

        }
    }
    [WebMethod]
    public static string[] SearchCities(string prefixText, int count)
    {
        using (POSEntities db = new POSEntities())
        {

            var cities = (from c in db.Cities where c.City_Name.Contains(prefixText) select c).ToArray();
            return cities.Select(c => AutoCompleteExtender.CreateAutoCompleteItem(c.City_Name, c.City_ID.ToString())).ToArray();
        }
    }

    protected void lbtnDeleteBranch_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;
        int cId = Convert.ToInt32(lbtn.CommandArgument);
        using (POSEntities db = new POSEntities())
        {
            var branch = (from c in db.Branches
                          where c.BranchId == cId
                          select c).FirstOrDefault();
            branch.IsDeleted = true;
            db.SaveChanges();
            BindBranches();
        }
    }

    protected void DDL_Company_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindBranches();
    }

    private void clearForm()
    {
        txtBranch.Text = "";
        txtCity.Text = "";
        hdfCity.Value = "";
        btnSaveBranch.Text = "Save";
        btnSaveBranch.CommandArgument = "";
    }
}